home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libprintui / printdialog.c.z / printdialog.c
C/C++ Source or Header  |  1996-05-06  |  15KB  |  548 lines

  1. /**************************************************************************
  2.  *                                      *
  3.  *           Copyright (c) 1991 Silicon Graphics, Inc.              *
  4.  *            All Rights Reserved                    *
  5.  *                                      *
  6.  *         THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI             *
  7.  *                                      *
  8.  * The copyright notice above does not evidence any actual of intended    *
  9.  * publication of such source code, and is an unpublished work by Silicon *
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is *
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or         *
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly *
  13.  * prohibited.                                  *
  14.  *                                      *
  15.  * RESTRICTED RIGHTS LEGEND:                          *
  16.  *                                      *
  17.  * Use, duplication or disclosure by the Government is subject to         *
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in   *
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,      *
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR     *
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of  *
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.       *
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311                          *
  24.  **************************************************************************
  25.  *
  26.  * File: printdialog.c
  27.  *
  28.  * Description: Presents a push button that brings up a PrintBox dialog
  29.  *    box when pressed.
  30.  *
  31.  **************************************************************************/
  32.  
  33.  
  34. #ident "$Revision: 1.5 $"
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <sys/types.h>
  42. #include <Xm/Xm.h>
  43. #include <X11/StringDefs.h>
  44. #include <Xm/RowColumn.h>
  45. #include <Xm/PushB.h>
  46. #include <Xm/MessageB.h>
  47. #include <Sgm/PrintBox.h>
  48.  
  49.  
  50. /* Program globals */
  51.  
  52. static char *prog_name;            /* Program instance name (no path) */
  53. static char *prog_class = "Printdialog";    /* Program class name */
  54. static Widget top_level;        /* Top level widget */
  55. static Widget print_box;        /* printBox widget */
  56. static Widget error_dialog;        /* Error dialog */
  57. static XtAppContext app_context;    /* Application context */
  58.  
  59.  
  60. /* Replacement error messages */
  61.  
  62. static char *no_spooler_mess[] = {
  63.     "The print scheduler /usr/lib/lpsched is not running.",
  64.     "Contact your system administrator."
  65.     };
  66. static int no_spooler_mess_lines = sizeof(no_spooler_mess) / sizeof(char*);
  67.  
  68.  
  69. /* Help message */
  70.  
  71. static char *help_mess[] = {
  72.     "'printdialog' simply instantiates a PrintBox dialog box."
  73.     };
  74. static int help_mess_lines = sizeof(help_mess) / sizeof(char*);
  75.  
  76. /* Local functions */
  77.  
  78. static void popup_cb(Widget, XtPointer, XtPointer);
  79. static void exit_cb(Widget, XtPointer, XtPointer);
  80. static void cancel_cb(Widget, XtPointer, XtPointer);
  81. static void print_cb(Widget, XtPointer, XtPointer);
  82. static void error_cb(Widget, XtPointer, XtPointer);
  83. static void help_cb(Widget, XtPointer, XtPointer);
  84. static Widget create_error_dialog(void);
  85. static XmString create_message(char**, int);
  86. static void manage_error_cb(Widget, XtPointer, XEvent*, Boolean*);
  87.  
  88.  
  89. /**************************************************************************
  90.  *
  91.  * Function: main
  92.  *
  93.  * Description: Program entry point
  94.  *
  95.  * Parameters: 
  96.  *    argc (I) - command line argument count
  97.  *    argv (I) - command line arguments
  98.  *
  99.  * Return: None
  100.  *
  101.  **************************************************************************/
  102.  
  103. int main(int argc, char *argv[])
  104. {
  105.     Widget rc, popup_button, exit_button;
  106.     char *str;
  107.  
  108.     /*
  109.      * Set program instance name
  110.      */
  111.     str = strrchr(*argv, '/');
  112.     prog_name = strdup((str == NULL) ? argv[0]: str + 1);
  113.  
  114.     /*
  115.      * Initialize the X app connection.
  116.      */
  117.     top_level = XtAppInitialize(&app_context, prog_class,
  118.                 NULL, 0, &argc, argv, NULL, NULL, 0);
  119.  
  120.     /*
  121.      * Create the top level manager widget
  122.      */
  123.     rc = XtCreateManagedWidget("buttonRC", xmRowColumnWidgetClass,
  124.                     top_level, NULL, 0);
  125.  
  126.     /*
  127.      * Create the popup push button
  128.      */
  129.     popup_button = XtCreateManagedWidget("popupButton",
  130.                     xmPushButtonWidgetClass, rc, NULL, 0);
  131.  
  132.     /*
  133.      * Create the exit push button
  134.      */
  135.     exit_button = XtCreateManagedWidget("exitButton",
  136.                     xmPushButtonWidgetClass, rc, NULL, 0);
  137.  
  138.     /*
  139.      * Add callbacks
  140.      */
  141.     XtAddCallback(exit_button, XmNactivateCallback, exit_cb, NULL);
  142.     XtAddCallback(popup_button, XmNactivateCallback, popup_cb, NULL);
  143.  
  144.     /*
  145.      * Realize all widgets
  146.      */
  147.     XtRealizeWidget(top_level);
  148.  
  149.     /*
  150.      * Start processing events.
  151.      */
  152.     XtAppMainLoop(app_context);
  153.  
  154.     /* NOTREACHED */
  155.     return(1);
  156. }
  157.  
  158.  
  159. /*=========================================================================
  160.                 LOCAL FUNCTIONS
  161.   =========================================================================*/
  162.  
  163.  
  164. /**************************************************************************
  165.  *
  166.  * Function: popup_cb
  167.  *
  168.  * Description: Called when the push button is pressed. The function
  169.  *    simply manages the PrintBox dialog box thereby poping it up.
  170.  *
  171.  * Parameters: 
  172.  *    w (I) - invoking widget
  173.  *    client_data (I) - client data
  174.  *    call_data (I) - standard callback info
  175.  *
  176.  * Return: none
  177.  *
  178.  **************************************************************************/
  179.  
  180. static void popup_cb(Widget w, XtPointer client_data, XtPointer call_data)
  181. {
  182.  
  183.     /*
  184.      * If the PrintBox dialog has not been created, create it
  185.      */
  186.     if (!print_box) {
  187.         Arg wargs[5];
  188.     register int n = 0;
  189.  
  190.         /*
  191.          * Create the PrintBox dialog.
  192.          */
  193.     XtSetArg(wargs[n], XmNautoUnmanage, False); n++;
  194.         print_box = PuiCreatePrintDialog(top_level, "printDialog", wargs, n);
  195.  
  196.         /*
  197.          * Add callbacks
  198.          */
  199.         XtAddCallback(print_box, PuiNcancelCallback, cancel_cb, NULL);
  200.         XtAddCallback(print_box, PuiNhelpCallback, help_cb, NULL);
  201.         XtAddCallback(print_box, PuiNjobInfoCallback, print_cb, NULL);
  202.         XtAddCallback(print_box, PuiNerrorCallback, error_cb, NULL);
  203.         XtAddCallback(print_box, PuiNoptionErrorCallback, error_cb, NULL);
  204.     }
  205.  
  206.     /*
  207.      * Pop up the dialog
  208.      */
  209.     if (!XtIsManaged(print_box))
  210.         XtManageChild(print_box);
  211. }
  212.  
  213.  
  214. /**************************************************************************
  215.  *
  216.  * Function: exit_cb
  217.  *
  218.  * Description: Causes the program to exit.
  219.  *
  220.  * Parameters: 
  221.  *    w (I) - invoking widget
  222.  *    client_data (I) - client data
  223.  *    call_data (I) - standard callback info
  224.  *
  225.  * Return: none
  226.  *
  227.  **************************************************************************/
  228.  
  229. static void exit_cb(Widget w, XtPointer client_data, XtPointer call_data)
  230. {
  231.     exit(0);
  232. }
  233.  
  234.  
  235. /**************************************************************************
  236.  *
  237.  * Function: cancel_cb
  238.  *
  239.  * Description: Cancel button popdown routine.
  240.  *
  241.  * Parameters: 
  242.  *    w (I) - invoking widget
  243.  *    client_data (I) - client data
  244.  *    call_data (I) - standard callback info
  245.  *
  246.  * Return: none
  247.  *
  248.  **************************************************************************/
  249. /* ARGSUSED */
  250.  
  251. static void cancel_cb(Widget w, XtPointer client_data, XtPointer call_data)
  252. {
  253.     if (XtIsManaged(print_box))
  254.         XtUnmanageChild(print_box);
  255. }
  256.  
  257.  
  258. /**************************************************************************
  259.  *
  260.  * Function: print_cb
  261.  *
  262.  * Description: Print job information callback. This callback is called
  263.  *    if a print job has been successfully submitted. The job ID is
  264.  *    printed to standard out and the program terminates.
  265.  *
  266.  * Parameters: 
  267.  *    w (I) - invoking widget
  268.  *    client_data (I) - client data
  269.  *    call_data (I) - standard callback info
  270.  *
  271.  * Return: none
  272.  *
  273.  **************************************************************************/
  274. /* ARGSUSED */
  275.  
  276. static void print_cb(Widget w, XtPointer client_data, XtPointer call_data)
  277. {
  278.     PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*)call_data;
  279.  
  280.     /*
  281.      * Print job ID
  282.      */
  283.     (void)printf("request id is %s\n", pb_cb->job_info->job_id);
  284.  
  285.     /*
  286.      * Pop down the printbox dialog
  287.      */
  288.     if (XtIsManaged(print_box))
  289.     XtUnmanageChild(print_box);
  290. }
  291.  
  292.  
  293. /**************************************************************************
  294.  *
  295.  * Function: error_cb
  296.  *
  297.  * Description: Handles the display of error messages from the printBox
  298.  *    widget.
  299.  *
  300.  * Parameters: 
  301.  *    w (I) - invoking widget
  302.  *    client_data (I) - client data
  303.  *    call_data (I) - standard callback info
  304.  *
  305.  * Return: none
  306.  *
  307.  **************************************************************************/
  308. /* ARGSUSED */
  309.  
  310. static void error_cb(Widget w, XtPointer client_data, XtPointer call_data)
  311. {
  312.     XmString str;
  313.     char **err_buf, *estr;
  314.     int num_str;
  315.     PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*)call_data;
  316.     Arg arg;
  317.  
  318.     /*
  319.      * If first time, create the error dialog window
  320.      */
  321.     if (!error_dialog)
  322.     error_dialog = create_error_dialog();
  323.  
  324.     /*
  325.      * Determine if this is an option panel error or printbox error
  326.      */
  327.     if (pb_cb->reason == PuiCR_OPT_ERROR) {
  328.     estr = strerror(pb_cb->error_code);
  329.     err_buf = &estr;
  330.     num_str = 1;
  331.     } else {
  332.     /*
  333.      * Select the appropriate message
  334.      */
  335.     switch (pb_cb->error_code) {
  336.         case SL_ERR_SPOOLER_ERROR:
  337.         SLGetSpoolerError(&err_buf, &num_str);
  338.         break;
  339.         case SL_ERR_NO_SPOOLERS:
  340.         case SL_ERR_SPOOLER_UNKNOWN:
  341.         XtAddCallback(error_dialog, XmNokCallback, cancel_cb, NULL);
  342.         err_buf = no_spooler_mess;
  343.         num_str = no_spooler_mess_lines;
  344.         break;
  345.         default:
  346.         estr = SLErrorString(pb_cb->error_code);
  347.         err_buf = &estr;
  348.         num_str = 1;
  349.         break;
  350.     }
  351.     }
  352.  
  353.     /*
  354.      * Set the message in the error dialog
  355.      */
  356.     str = create_message(err_buf, num_str);
  357.     XtSetArg(arg, XmNmessageString, str);
  358.     XtSetValues(error_dialog, &arg, 1);
  359.     XmStringFree(str);
  360.  
  361.     /*
  362.      * Bring up the dialog. If the PrintBox dialog is up we simply manage
  363.      * the error dialog. Otherwise we watch for it to show up and then
  364.      * post the dialog.
  365.      */
  366.     if (XtIsRealized(print_box))
  367.         XtManageChild(error_dialog);
  368.     else
  369.     XtAddEventHandler(print_box, VisibilityChangeMask, False,
  370.                         manage_error_cb, NULL);
  371. }
  372.  
  373.  
  374. /**************************************************************************
  375.  *
  376.  * Function: manage_error_cb
  377.  *
  378.  * Description: Handles the delayed display of an error dialog. The
  379.  *    delay was due to the PrintBox dialog not being visible when
  380.  *    the error occurred. We want it visible so that the error dialog
  381.  *    does not get hidden behind the PrintBox.
  382.  *
  383.  * Parameters: 
  384.  *    w (I) - PrintBox widget
  385.  *    client_data (I) - not used
  386.  *    event (I) - event that triggered the call
  387.  *    cont (I) - continue propagating event flag
  388.  *
  389.  * Return: none
  390.  *
  391.  **************************************************************************/
  392.  
  393. static void manage_error_cb(Widget w, XtPointer client_data, XEvent *event,
  394.                                 Boolean *cont)
  395. {
  396.     XtRemoveEventHandler(print_box, VisibilityChangeMask, False,
  397.                         manage_error_cb, NULL);
  398.     XtManageChild(error_dialog);
  399. }
  400.  
  401.  
  402. /**************************************************************************
  403.  *
  404.  * Function: help_cb
  405.  *
  406.  * Description: Displays the help message.
  407.  *
  408.  * Parameters: 
  409.  *    w (I) - invoking widget
  410.  *    client_data (I) - not used
  411.  *    call_data (I) - not used
  412.  *
  413.  * Return: none
  414.  *
  415.  **************************************************************************/
  416. /* ARGSUSED */
  417.  
  418. static void help_cb(Widget w, XtPointer client_data, XtPointer call_data)
  419. {
  420.     static Widget help_dialog = NULL;
  421.  
  422.     /*
  423.      * If first time, create the help dialog
  424.      */
  425.     if (!help_dialog) {
  426.         Arg wargs[5];
  427.         XmString tstr, mstr;
  428.         register int n;
  429.  
  430.         /*
  431.          * Create title and message strings
  432.          */
  433.         tstr = XmStringCreateSimple("Help");
  434.     mstr = create_message(help_mess, help_mess_lines);
  435.  
  436.         /*
  437.          * Create a dialog
  438.          */
  439.         n = 0;
  440.         XtSetArg(wargs[n], XmNdialogTitle, tstr); n++;
  441.         XtSetArg(wargs[n], XmNmessageString, mstr); n++;
  442.         help_dialog = XmCreateInformationDialog(top_level, "helpDialog",
  443.                                 wargs, n);
  444.  
  445.         /*
  446.          * Blow off Help and Cancel
  447.          */
  448.         XtUnmanageChild(XmMessageBoxGetChild(help_dialog,
  449.                         XmDIALOG_CANCEL_BUTTON));
  450.         XtUnmanageChild(XmMessageBoxGetChild(help_dialog,
  451.                         XmDIALOG_HELP_BUTTON));
  452.  
  453.         XmStringFree(tstr);
  454.         XmStringFree(mstr);
  455.     }
  456.  
  457.     /*
  458.      * Display the dialog
  459.      */
  460.     XtManageChild(help_dialog);
  461. }
  462.  
  463.  
  464. /**************************************************************************
  465.  *
  466.  * Function: create_error_dialog
  467.  *
  468.  * Description: Creates the error dialog 
  469.  *
  470.  * Parameters: none
  471.  *
  472.  * Return: Created dialog widget
  473.  *
  474.  **************************************************************************/
  475.  
  476. static Widget create_error_dialog()
  477. {
  478.     Widget dialog;
  479.     Arg wargs[5];
  480.     XmString tstr;
  481.     register int n;
  482.  
  483.     /*
  484.      * Create some compound strings
  485.      */
  486.     tstr = XmStringCreateSimple("Error");    /* Dialog window title */
  487.  
  488.     /*
  489.      * Create a modal dialog
  490.      */
  491.     n = 0;
  492.     XtSetArg(wargs[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
  493.     XtSetArg(wargs[n], XmNdialogTitle, tstr); n++;
  494.     dialog = XmCreateErrorDialog(print_box, "errorDialog", wargs, n);
  495.  
  496.     /*
  497.      * Blow off Help and Cancel
  498.      */
  499.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
  500.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
  501.  
  502.     XmStringFree(tstr);
  503.     
  504.     return dialog;
  505. }
  506.  
  507.  
  508. /**************************************************************************
  509.  *
  510.  * Function: create_message
  511.  *
  512.  * Description: A utility function to create a compound string message from
  513.  *      a multi-element array of strings.
  514.  *
  515.  * Parameters:
  516.  *      str_array (I) - array of message strings.
  517.  *    num_str (I) - number of strings in array.
  518.  *
  519.  * Return: A compound string.
  520.  *
  521.  **************************************************************************/
  522.  
  523. static XmString create_message(char **str_array, int num_str)
  524. {
  525.     XmString xm_newstr, xtemp, xmstr;
  526.     register char *str;
  527.     register int i;
  528.  
  529.     xmstr = XmStringCreateSimple("");
  530.     for (i = 0, str = *str_array; i < num_str; str = *++str_array, i++) {
  531.     if (i) {
  532.             xm_newstr = XmStringSeparatorCreate();
  533.             xtemp = xmstr;
  534.         xmstr = XmStringConcat(xtemp, xm_newstr);
  535.         XmStringFree(xtemp);
  536.         XmStringFree(xm_newstr);
  537.     }
  538.     xm_newstr = XmStringCreateSimple(str);
  539.     xtemp = xmstr;
  540.     xmstr = XmStringConcat(xtemp, xm_newstr);
  541.     XmStringFree(xtemp);
  542.     XmStringFree(xm_newstr);
  543.     }
  544.  
  545.     return xmstr;
  546. }
  547.  
  548.